package com.algorithm.binarysearch;

public class PostKey {
    public static void main(String[] args) {
        Node head = new Node(0, null);
        int[] arr = {1, 2, 3, 4, 5, 6};
        Node p = head;
        for (int i = 0; i < arr.length; i++) {
            Node node = new Node(arr[i], null);
            p.setNext(node);
            p = node;
        }
        move(head, 3);
        print(head);
    }

    public static void move(Node head, int k) {
        k = k % 6;
        Node pre = head;
        for (int i = 0; i < k; i++) {
            pre = pre.getNext();

        }
        Node post = head;
        while (pre.getNext() != null) {
            pre = pre.getNext();
            post = post.getNext();
        }
        Node p = head.getNext();
        head.setNext(post.getNext());
        pre.setNext(p);
        post.setNext(null);
    }

    public static void print(Node head) {
        Node p = head;
        while (p.getNext() != null) {
            System.out.println(p.getNext().getKey());
            p = p.getNext();
        }
    }
}

class Node {
    private int key;
    private Node next;

    public int getKey() {
        return key;
    }

    public void setKey(int key) {
        this.key = key;
    }

    public Node getNext() {
        return next;
    }

    public void setNext(Node next) {
        this.next = next;
    }

    public Node(int key, Node next) {
        this.key = key;
        this.next = next;
    }

    public Node() {
    }
}

